package controllers;

import play.mvc.*;
import play.data.binding.Global;
import play.data.binding.TypeBinder;
import play.libs.*;

import java.io.FileReader;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.*;


import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.sun.xml.internal.ws.api.message.Message;


import models.*;
@With (Secure.class)
public class Application extends Controller {

    public static void index() {
    	String x= "osama"; 
        render(x);
    }
    public static void test(String name , String id) {
    	ArrayList<User> allusers =(ArrayList) User.findAll();
    	render(name,id,allusers);
    }
    public static void jsonusers(String name , String id) {
    	ArrayList<User> allusers =(ArrayList) User.findAll();
    	renderJSON(allusers);
        
    }
    
    public static void Update(String  users)
    {
    	String json = users.replace('[', ' ').replace(']', ' ');
    	String[] emparr=json.trim().split("},");
    	
   	for(int i=0;i < emparr.length ;i++)
   	{
   		String cleamemp="";
   		if(i==(emparr.length-1))
   		{
   			
   			cleamemp=emparr[i].trim();
   		}
   		else
   		{
   			cleamemp=emparr[i].trim()+"}";
   			
   			
   		}
   		Gson gson = new Gson();
   		Type type = new TypeToken<HashMap<String, String>>(){}.getType();
   		HashMap<String, String> map = new HashMap<String, String>();
   		map = gson.fromJson( cleamemp.trim(), type );
   		User updated= User.findById(Long.parseLong(map.get("id"), 10));
      	updated.username=map.get("username");
        updated.password=map.get("password");
       	updated.save();
   		
  		
   	}
    	
    	
    	
    	
    		
    	
    	
   
    }
    public static void Create(String  users)
    {
    	
    	String json = users.replace('[', ' ').replace(']', ' ');
    	String[] emparr=json.trim().split("},");
    	
   	for(int i=0;i < emparr.length ;i++)
   	{
   		String cleamemp="";
   		if(i==(emparr.length-1))
   		{
   			
   			cleamemp=emparr[i].trim();
   		}
   		else
   		{
   			cleamemp=emparr[i].trim()+"}";
   			
   			
   		}
   		Gson gson = new Gson();
   		Type type = new TypeToken<HashMap<String, String>>(){}.getType();
   		HashMap<String, String> map = new HashMap<String, String>();
   		map = gson.fromJson( cleamemp.trim(), type );
   		User created= new User(map.get("username"),map.get("password"));
    	created.save();
   		
  		
   	}
    	
    	
    	
    
    }
    public static void Delete(String  users)
    {
    	String json = users.replace('[', ' ').replace(']', ' ');
    	String[] emparr=json.trim().split("},");
    	
   	for(int i=0;i < emparr.length ;i++)
   	{
   		String cleamemp="";
   		if(i==(emparr.length-1))
   		{
   			
   			cleamemp=emparr[i].trim();
   		}
   		else
   		{
   			cleamemp=emparr[i].trim()+"}";
   			
   			
   		}
   		Gson gson = new Gson();
   		Type type = new TypeToken<HashMap<String, String>>(){}.getType();
   		HashMap<String, String> map = new HashMap<String, String>();
   		map = gson.fromJson( cleamemp.trim(), type );
   		User deleted= User.findById(Long.parseLong(map.get("id"), 10));
    	deleted.delete();
   		
  		
   	}
    	
    	
    	
    
    
    }
    
}

